package com.apache.rpc.service.monitoring;

import com.apache.api.vo.ParamsVo;
import com.apache.database.constant.SpringContextLoader;
import com.apache.method.SystemRunnable;
import com.apache.method.ThreadPool;
import com.apache.rpc.entity.RpcMonitoring;
import com.apache.rpc.manager.RpcMonitoringManager;

/**
 * description: 监控信保存线程
 *
 * @author dyh 创建时间：2018年03月09日
 */
public class MonitoringDataBaseHelper {

    private ThreadPool taskPool;

    private static MonitoringDataBaseHelper instance;

    private MonitoringDataBaseHelper() {
        taskPool = new ThreadPool(3, "监控线程");
    }

    public synchronized static MonitoringDataBaseHelper getInstance() {
        if (null == instance) {
            instance = new MonitoringDataBaseHelper();
        }
        return instance;
    }

    public void exeMonitoring(RpcMonitoring monitoring) {
        MonitoringDataBaseRunable runable = new MonitoringDataBaseRunable(monitoring);
        taskPool.execute(runable);
    }

    class MonitoringDataBaseRunable extends SystemRunnable {

        private RpcMonitoring monitoring;

        public MonitoringDataBaseRunable(RpcMonitoring monitoring) {
            this.monitoring = monitoring;
        }

        public void run() {
            try {
                ParamsVo vo = new ParamsVo();
                RpcMonitoringManager manager = (RpcMonitoringManager) SpringContextLoader
                        .getBean("rpcMonitoringManager");
                vo.setObj(monitoring);
                manager.saveInfo(vo);
            } catch (Exception e) {
            }
        }
    }
}
